package com.software.loginregister.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.software.loginregister.R;

import com.software.loginregister.entity.vo.ImageFoodGroupVO;
import com.software.loginregister.entity.vo.RefrigeratorFoodVO;

import java.util.ArrayList;

import java.util.List;

public class FridgeExpandableAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<ImageFoodGroupVO> groupList; // 修改为直接存储 ImageFoodGroupVO
    private static final String TAG = "FridgeExpandableAdapter";
    private OnDeleteClickListener onDeleteClickListener;
    private OnDetailClickListener onDetailClickListener;

    public FridgeExpandableAdapter(Context context) {
        this.context = context;
        this.groupList = new ArrayList<>();
    }

    // 添加接口定义
    public interface OnDeleteClickListener {
        void onDelete(Long foodId);
    }

    public interface OnDetailClickListener {
        void onDetail(RefrigeratorFoodVO food);
    }

    // 设置监听器的方法
    public void setOnDeleteClickListener(OnDeleteClickListener listener) {
        this.onDeleteClickListener = listener;
    }

    public void setOnDetailClickListener(OnDetailClickListener listener) {
        this.onDetailClickListener = listener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupList.get(groupPosition).getFoods().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).getFoods().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    // 获取组的时间
    public String getGroupTime(int groupPosition) {
        return groupList.get(groupPosition).getCreateTime();
    }

    // 获取组内的所有食物
    public List<RefrigeratorFoodVO> getGroupFoods(int groupPosition) {
        return groupList.get(groupPosition).getFoods();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fridge_group, parent, false);
            groupHolder = new GroupViewHolder();
            groupHolder.ivFoodImage = convertView.findViewById(R.id.fridge_group_iv_food_image);
            groupHolder.tvCaptureTime = convertView.findViewById(R.id.fridge_group_tv_capture_time);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupViewHolder) convertView.getTag();
        }

        ImageFoodGroupVO group = groupList.get(groupPosition);
        groupHolder.tvCaptureTime.setText(group.getCreateTime());

        // 加载组的图片
        if (group.getPhotoUrl() != null && !group.getPhotoUrl().isEmpty()) {
            Glide.with(context)
                .load(group.getPhotoUrl())
                .placeholder(R.drawable.ic_food_icon)
                .error(R.drawable.ic_food_icon)
                .centerCrop()
                .into(groupHolder.ivFoodImage);
        } else {
            Glide.with(context)
                .load(R.drawable.ic_food_icon)
                .into(groupHolder.ivFoodImage);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fridge_child, parent, false);
            childHolder = new ChildViewHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildViewHolder) convertView.getTag();
        }

        RefrigeratorFoodVO food = groupList.get(groupPosition).getFoods().get(childPosition);
        
        try {
            if (food != null) {
                childHolder.tvFoodName.setText(food.getFoodName());

                Log.d(TAG, "Displaying food: " + food.getFoodName());
            }
        } catch (Exception e) {
            Log.e(TAG, "Error displaying food item", e);
        }

        childHolder.btnDelete.setOnClickListener(v -> {
            if (onDeleteClickListener != null) {
                onDeleteClickListener.onDelete(food.getId());
            }
        });

        childHolder.tvDetail.setOnClickListener(v -> {
            if (onDetailClickListener != null) {
                onDetailClickListener.onDetail(food);
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    // 更新数据的新方法
    public void updateDataWithGroups(List<ImageFoodGroupVO> groups) {
        if (groups == null) {
            Log.e(TAG, "updateDataWithGroups: groups list is null");
            return;
        }

        Log.d(TAG, "updateDataWithGroups: received " + groups.size() + " groups");
        this.groupList.clear();
        this.groupList.addAll(groups);
        notifyDataSetChanged();
    }

    static class GroupViewHolder {
        ImageView ivFoodImage;
        TextView tvCaptureTime;
    }

    static class ChildViewHolder {
        TextView tvFoodName;
        TextView tvDetail;
        ImageButton btnDelete;

        ChildViewHolder(View view) {  // 添加构造函数
            tvFoodName = view.findViewById(R.id.fridge_child_tv_food_name);
            tvDetail = view.findViewById(R.id.fridge_child_tv_food_detail);
            btnDelete = view.findViewById(R.id.fridge_child_btn_delete);
        }

    }
}